package com.deyuanyun.pic.pipe.support.validatefun;

import com.caixu.beanvalidateutil.vinterface.ValidateInterface;
import com.deyuanyun.pic.support.biz.CodeUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Field;
import java.util.List;

/**
 * Created by xu on 2015/12/29.
 */
public class PipeCrossVali implements ValidateInterface {
    @Override
    public <T, E,C> boolean apply(T t, E e,C c) {
        Field field= (Field) e;
        String fName=field.getName();
        if ("crossingType".equals(fName)){//穿跨越类型
            List<String> ss= CodeUtil.getCodeListString("CROSSING_TYPE");
            if (ss.contains(t)){
                String id_ = CodeUtil.getCodeId("CROSSING_TYPE",(String) t);
                try {
                    BeanUtils.setProperty(c,fName,id_);
                } catch (Exception e1) {
                    e1.printStackTrace();
                    return false;
                }
                return true;
            }
        }else if ("crossingTarget".equals(fName)){//穿跨越对象
            List<String> ss= CodeUtil.getCodeListString("CROSSING_TARGET");
            if (ss.contains(t)){
                String id_ = CodeUtil.getCodeId("CROSSING_TARGET",(String) t);
                try {
                    BeanUtils.setProperty(c,fName,id_);
                } catch (Exception e1) {
                    e1.printStackTrace();
                    return false;
                }
                return true;
            }
        }else if ("crossingMethod".equals(fName)){//穿跨越方式
            List<String> ss= CodeUtil.getCodeListString("CROSSING_METHOD");
            if (ss.contains(t)){
                String id_ = CodeUtil.getCodeId("CROSSING_METHOD",(String) t);
                try {
                    BeanUtils.setProperty(c,fName,id_);
                } catch (Exception e1) {
                    e1.printStackTrace();
                    return false;
                }
                return true;
            }
        }else if ("material".equals(fName)){//材质
            List<String> ss= CodeUtil.getCodeListString("MATERIAL");
            if (ss.contains(t)){
                String id_ = CodeUtil.getCodeId("MATERIAL",(String) t);
                try {
                    BeanUtils.setProperty(c,fName,id_);
                } catch (Exception e1) {
                    e1.printStackTrace();
                    return false;
                }
                return true;
            }
            String ts=(String)t;
            if (StringUtils.endsWith(ts,".0")){
                ts=StringUtils.removeEnd(ts,".0");
                if (ss.contains(ts)){
                    String id_ = CodeUtil.getCodeId("MATERIAL",(String) ts);
                    try {
                        BeanUtils.setProperty(c,fName,id_);
                    } catch (Exception e1) {
                        e1.printStackTrace();
                        return false;
                    }
                    return true;
                }
            }
        }else if ("pipeStableMeasures".equals(fName)){//稳管措施
            List<String> ss= CodeUtil.getCodeListString("PIPE_STABLE_MEASURES");
            if (ss.contains(t)){
                String id_ = CodeUtil.getCodeId("PIPE_STABLE_MEASURES",(String) t);
                try {
                    BeanUtils.setProperty(c,fName,id_);
                } catch (Exception e1) {
                    e1.printStackTrace();
                    return false;
                }
                return true;
            }
        }else if ("isSleeve".equals(fName)){//是否有套管
            List<String> ss= CodeUtil.getCodeListString("IS_SLEEVE");
            if (ss.contains(t)){
                String id_ = CodeUtil.getCodeId("IS_SLEEVE",(String) t);
                try {
                    BeanUtils.setProperty(c,fName,id_);
                } catch (Exception e1) {
                    e1.printStackTrace();
                    return false;
                }
                return true;
            }
        }/*else if ("coatingMaterial".equals(fName)){//防护层材料
            List<String> ss= CodeUtil.getCodeListString("COATING_MATERIAL_CROSS");
            if (ss.contains(t)){
                return true;
            }
        }*/
        return false;
    }

    @Override
    public String getName() {
        return this.getName();
    }
}
